/*****************************************************************************************
 * FILE NAME: test_chaodian_can_L                        COPYRIGHT (c) TX 2025
 * REVISION:  1.0.0                                     All Rights Reserved
 * DESCRIPTION: 超电CAN协议模块单元测试头文件
 * =======================================================================================
 * UPDATE HISTORY：
 * REV      AUTHOR      DATE
 * v1.0.0   fangangchuan  2025.10.15
 * DESCRIPTION OF CHANGE: 创建超电CAN协议模块的单元测试框架
 ****************************************************************************************/
#ifndef __TEST_CHAODIAN_CAN_L_H
#define __TEST_CHAODIAN_CAN_L_H

//----------------------------------------------------------------------------------------
// @Project Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "srv_chaodian_can_L.h"

//----------------------------------------------------------------------------------------
// @Global Macro Define in the module

/* 测试框架宏定义 */
#define TEST_ASSERT(condition) \
    do { \
        if (!(condition)) { \
            printf("TEST FAILED: %s:%d - %s\n", __FILE__, __LINE__, #condition); \
            test_failed_count++; \
            return; \
        } \
    } while(0)

#define TEST_ASSERT_EQUAL(expected, actual) \
    do { \
        if ((expected) != (actual)) { \
            printf("TEST FAILED: %s:%d - Expected: %d, Actual: %d\n", \
                   __FILE__, __LINE__, (int)(expected), (int)(actual)); \
            test_failed_count++; \
            return; \
        } \
    } while(0)

#define TEST_ASSERT_EQUAL_UINT8(expected, actual) \
    do { \
        if ((expected) != (actual)) { \
            printf("TEST FAILED: %s:%d - Expected: 0x%02X, Actual: 0x%02X\n", \
                   __FILE__, __LINE__, (uint8_t)(expected), (uint8_t)(actual)); \
            test_failed_count++; \
            return; \
        } \
    } while(0)

#define TEST_ASSERT_EQUAL_UINT16(expected, actual) \
    do { \
        if ((expected) != (actual)) { \
            printf("TEST FAILED: %s:%d - Expected: 0x%04X, Actual: 0x%04X\n", \
                   __FILE__, __LINE__, (uint16_t)(expected), (uint16_t)(actual)); \
            test_failed_count++; \
            return; \
        } \
    } while(0)

#define TEST_ASSERT_EQUAL_UINT32(expected, actual) \
    do { \
        if ((expected) != (actual)) { \
            printf("TEST FAILED: %s:%d - Expected: 0x%08X, Actual: 0x%08X\n", \
                   __FILE__, __LINE__, (uint32_t)(expected), (uint32_t)(actual)); \
            test_failed_count++; \
            return; \
        } \
    } while(0)

#define TEST_ASSERT_EQUAL_STRING(expected, actual, length) \
    do { \
        if (memcmp((expected), (actual), (length)) != 0) { \
            printf("TEST FAILED: %s:%d - String comparison failed\n", __FILE__, __LINE__); \
            test_failed_count++; \
            return; \
        } \
    } while(0)

#define TEST_ASSERT_NULL(ptr) \
    do { \
        if ((ptr) != NULL) { \
            printf("TEST FAILED: %s:%d - Expected NULL, got non-NULL pointer\n", \
                   __FILE__, __LINE__); \
            test_failed_count++; \
            return; \
        } \
    } while(0)

#define TEST_ASSERT_NOT_NULL(ptr) \
    do { \
        if ((ptr) == NULL) { \
            printf("TEST FAILED: %s:%d - Expected non-NULL pointer, got NULL\n", \
                   __FILE__, __LINE__); \
            test_failed_count++; \
            return; \
        } \
    } while(0)

/* 测试统计宏 */
#define TEST_START() \
    do { \
        printf("Starting test: %s\n", __FUNCTION__); \
        test_start_time = mock_drv_systick_getTick(); \
    } while(0)

#define TEST_END() \
    do { \
        uint32_t test_duration = mock_drv_systick_getTick() - test_start_time; \
        printf("Test completed: %s (Duration: %d ms)\n", __FUNCTION__, test_duration); \
        test_passed_count++; \
    } while(0)

/* 测试套件宏 */
#define TEST_SUITE_START(name) \
    do { \
        printf("\n=== Starting Test Suite: %s ===\n", name); \
        test_suite_start_time = mock_drv_systick_getTick(); \
    } while(0)

#define TEST_SUITE_END(name) \
    do { \
        uint32_t suite_duration = mock_drv_systick_getTick() - test_suite_start_time; \
        printf("=== Test Suite Completed: %s (Duration: %d ms) ===\n", name, suite_duration); \
        printf("Passed: %d, Failed: %d\n\n", test_passed_count, test_failed_count); \
    } while(0)

/* 模拟CAN发送队列相关宏 */
#define CAN_MAX_TX_SIZE 16
#ifndef CAN_ID_EXT
#define CAN_ID_EXT 1
#endif
#ifndef CAN_RTRQ_DATA
#define CAN_RTRQ_DATA 0
#endif
#define CAN_TXDLC_8 8

/* 模拟CAN消息结构体 */
typedef struct {
    uint32_t ExtId;
    uint8_t IDE;
    uint8_t RTR;
    uint8_t DLC;
    uint8_t Data[8];
} stc_can_message_t;

/* 模拟CAN全局变量 */
typedef struct {
    stc_can_message_t canTxMessage[CAN_MAX_TX_SIZE];
    uint8_t vu8canTxMQPushLen;
} stc_can_t;

//----------------------------------------------------------------------------------------
// @Global Type defines in the module

/* 测试统计结构体 */
typedef struct {
    uint32_t total_tests;
    uint32_t passed_tests;
    uint32_t failed_tests;
    uint32_t total_time_ms;
} stc_test_stats_t;

//----------------------------------------------------------------------------------------
// @Export global Variables declare in the module

/* 测试统计变量 */
extern uint32_t test_passed_count;
extern uint32_t test_failed_count;
extern uint32_t test_start_time;
extern uint32_t test_suite_start_time;

/* 模拟CAN全局变量 */
extern stc_can_t g_stccan;

/* 模拟全局变量 */
extern uint16_t g_test_speed_value;
extern uint16_t g_test_gps_speed_limit_value;
extern uint8_t g_test_init_finish_flag;
extern char g_test_iccid_sn[20];
extern char g_test_imei_sn[16];
extern uint8_t g_test_ble_mac[6];

//----------------------------------------------------------------------------------------
// @Export declaration of functions in the module

/* 测试框架函数 */
void test_framework_init(void);
void test_framework_cleanup(void);
void test_framework_print_stats(void);
stc_test_stats_t test_framework_get_stats(void);

/* 模拟函数 */
uint32_t mock_drv_systick_getTick(void);
void mock_drv_systick_setTick(uint32_t tick);
void mock_can_reset(void);
uint8_t mock_can_get_tx_count(void);
stc_can_message_t* mock_can_get_tx_message(uint8_t index);

/* 测试辅助函数 */
void test_setup_mock_data(void);
void test_cleanup_mock_data(void);
void test_print_can_message(const stc_can_message_t* msg);
void test_print_hex_data(const uint8_t* data, uint8_t length);

/* 基础工具函数测试 */
void test_cd_can_build_id(void);
void test_cd_can_parse_id(void);
void test_cd_can_is_for_me(void);
void test_cd_can_find_reg_map(void);

/* 数据转换函数测试 */
void test_cd_transform_speed(void);
void test_cd_transform_gps_speed_limit(void);

/* 数据读写函数测试 */
void test_cd_can_read_data_from_ptr(void);
void test_cd_can_write_data_to_ptr(void);

/* 寄存器操作函数测试 */
void test_cd_can_read_register(void);
void test_cd_can_write_register(void);
void test_cd_can_is_reg_readable(void);
void test_cd_can_is_reg_writable(void);
void test_cd_can_performance_optimization(void);
void test_cd_can_direct_function_calls(void);

/* 发送响应函数测试 */
void test_cd_can_send_read_response(void);
void test_cd_can_send_write_response(void);
void test_cd_can_send_error_response(void);
void test_cd_can_send_event_report(void);
void test_cd_can_send_multi_frame_data(void);

/* 写入处理函数测试 */
void test_cd_can_process_single_frame_write(void);
void test_cd_can_write_multi_register_range(void);
void test_cd_can_process_multi_frame_write(void);

/* 消息处理函数测试 */
void test_cd_can_process_read_request(void);
void test_cd_can_process_write_request(void);
void test_cd_can_process_upgrade_message(void);

/* 升级相关函数测试 */
void test_cd_can_start_upgrade(void);
void test_cd_can_process_upgrade_data(void);
void test_cd_can_finish_upgrade(void);
void test_cd_can_get_upgrade_state(void);

/* 统计信息函数测试 */
void test_cd_can_get_stats(void);
void test_cd_can_reset_stats(void);

/* 对外接口函数测试 */
void test_cd_can_init(void);
void test_cd_can_process_message(void);
void test_cd_can_task(void);

/* 升级流程生产测试 */
void test_cd_can_complete_upgrade_flow(void);
void test_cd_can_upgrade_timeout_scenarios(void);
void test_cd_can_upgrade_data_integrity(void);

/* 测试套件 */
void test_suite_basic_functions(void);
void test_suite_data_transform(void);
void test_suite_register_operations(void);
void test_suite_message_processing(void);
void test_suite_upgrade_functions(void);
void test_suite_integration(void);
void test_suite_upgrade_production(void);

#endif

// =======================================================================================
// End of file.
// =======================================================================================
